﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.Mesh.CUiMeshUtil
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Paint.Mesh;

public class CUiMeshUtil
{
  private static string m_distanceRegistryCaption = "Distance";
  private static string m_angleRegistryCaption = "Angle";
  private static string m_deviationRegistryCaption = "Deviation";

  internal static TxObjectList FilterSelection()
  {
    TxObjectList items = TxApplication.ActiveSelection.GetItems();
    TxObjectList objList = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) items)
    {
      int num;
      switch (itxObject)
      {
        case null:
          num = 0;
          break;
        case TxComponent _:
        case TxCompoundResource _:
        case TxCompoundPart _:
          num = 1;
          break;
        default:
          num = itxObject is TxDevice ? 1 : 0;
          break;
      }
      if (num != 0 && !((Collection<ITxObject>) objList).Contains(itxObject))
        ((Collection<ITxObject>) objList).Add(itxObject);
    }
    return CUiMeshUtil.FindParts(objList);
  }

  public static TxObjectList FindParts(TxObjectList objList)
  {
    TxObjectList parts = new TxObjectList();
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (TxComponent));
    foreach (ITxObject itxObject in (Collection<ITxObject>) objList)
    {
      if (itxObject is TxCompoundResource || itxObject is TxCompoundPart)
      {
        foreach (ITxObject allDescendant in (Collection<ITxObject>) (itxObject as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) txTypeFilter))
        {
          if (!((Collection<ITxObject>) parts).Contains(allDescendant))
            ((Collection<ITxObject>) parts).Add(allDescendant);
        }
      }
      else if (!((Collection<ITxObject>) parts).Contains(itxObject))
        ((Collection<ITxObject>) parts).Add(itxObject);
    }
    return parts;
  }

  public static bool DoesPartHaveExactGeometryOnly(ITxObject part)
  {
    bool flag = false;
    if (TxComponent.op_Inequality(part as TxComponent, (ITxObject) null))
      flag = TxComponentEx.DoesHaveExactGeometryOnly((ITxComponent) part);
    return flag;
  }

  public static bool DoesPartHaveMesh(ITxObject part)
  {
    bool flag = false;
    if (part is ITxComponent)
    {
      if (TxMeshComponentEx.DoesPartHaveMesh(part as ITxComponent))
        flag = true;
    }
    else
    {
      foreach (ITxComponent allDescendant in (Collection<ITxObject>) (part as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxComponent))))
      {
        if (TxMeshComponentEx.DoesPartHaveMesh(allDescendant))
        {
          flag = true;
          break;
        }
      }
    }
    return flag;
  }

  public static void StoreValuesToRegistry(TxWindow window, CApMeshManager data)
  {
    if (data == null)
      return;
    TxWindowSettings.StoreWindowValue(window, CUiMeshUtil.m_deviationRegistryCaption, (object) data.Deviation);
    TxWindowSettings.StoreWindowValue(window, CUiMeshUtil.m_distanceRegistryCaption, (object) data.Distance);
    TxWindowSettings.StoreWindowValue(window, CUiMeshUtil.m_angleRegistryCaption, (object) data.Angle);
  }

  public static void LoadValuesFromRegistry(TxWindow window, ref CApMeshManager data)
  {
    if (data == null)
      return;
    object s1;
    TxWindowSettings.RestoreWindowValue(window, CUiMeshUtil.m_deviationRegistryCaption, ref s1);
    if (s1 != null)
    {
      double result = CApMeshManager.DEVIATION_DEFAULT_VALUE;
      if (double.TryParse(s1 as string, out result))
        data.Deviation = result;
    }
    object s2;
    TxWindowSettings.RestoreWindowValue(window, CUiMeshUtil.m_distanceRegistryCaption, ref s2);
    if (s2 != null)
    {
      double result = CApMeshManager.DISTANCE_DEFAULT_VALUE;
      if (double.TryParse(s2 as string, out result))
        data.Distance = result;
    }
    object s3;
    TxWindowSettings.RestoreWindowValue(window, CUiMeshUtil.m_angleRegistryCaption, ref s3);
    if (s3 == null)
      return;
    double result1 = CApMeshManager.ANGLE_DEFAULT_VALUE;
    if (!double.TryParse(s3 as string, out result1))
      return;
    data.Angle = result1;
  }

  public static ImageSource GetIconImage(string uriPath)
  {
    Uri uriSource = new Uri(uriPath, UriKind.Absolute);
    return uriSource == (Uri) null ? (ImageSource) null : (ImageSource) new BitmapImage(uriSource);
  }

  public static ImageSource XIconImage()
  {
    return CUiMeshUtil.GetIconImage("pack://application:,,,/DnProcessSimulateCommands;component/Paint/Mesh/Resources/failure.png");
  }

  public static ImageSource VIconImage()
  {
    return CUiMeshUtil.GetIconImage("pack://application:,,,/DnProcessSimulateCommands;component/Paint/Mesh/Resources/success.png");
  }

  public static ImageSource DashIconImage()
  {
    return CUiMeshUtil.GetIconImage("pack://application:,,,/DnProcessSimulateCommands;component/Paint/Mesh/Resources/none.png");
  }
}
